import glob
import ntpath
import pandas as pd
from MyConfig import MyConfig

if __name__=="__main__":
    try:
        settings = MyConfig("consolidation.json")
        handlers:list = settings.get("handlers",[])
        shop = "prokat_z930"
        input_file = "D:\\work\\Укрупнение\\current\\debug_data\\[прокат][Z930].xlsx"
        out_pattern ="D:\\work\\Укрупнение\\current\\debug_data2\\"
        mapping=pd.read_excel("D:\\work\\Укрупнение\\current\\справочники4\\Маппинг производств.xlsx")
        df = pd.read_excel(input_file)
        df = df.merge(mapping,left_on="Производство",right_on="Производство",how="left")
        
        for item in handlers:
            dfh:pd.DataFrame = df[df["Производство2"]==item["key"]]
            if not dfh.empty:
                dfh.drop("Производство2",axis=1,inplace=True)
                dfh.to_excel(f"{out_pattern}{shop}_{item['module']}.xlsx",index=False)
                print(f"{item['key']} : {item['module']} : done")


    except Exception as exp:
        print(f"error: {exp}")